using System;
using System.Collections;
using System.Collections.Generic;
using Pathfinding;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 状态管理机，定义敌人可能的状态
/// </summary>
public enum StateType
{
    Chase, // 追击
    Attack, // 攻击
    Hurt, // 受击
    Cooldown, // 冷却
    Death // 死亡状态
}

/// <summary>
/// 敌人属性类，包含各种属性的定义
/// </summary>
[System.Serializable]
public class Parameter
{
    public float enemyCurrentHealth; // 当前血量
    public float enemyMaxHealth; // 最大血量
    public float enemyAttackValue; // 攻击数值
    public float enemyAttackSpeed; // 攻击速度
    public float enemyMoveSpeed; // 移动速度
    public float enemyDistortionLevel; // 失真度
    public float enemyAttackDistance; // 攻击范围
    public float enemyAttackCooldown; // 攻击冷却时间
    public Animator enemyAnimator; // 动画控制器
    public float enemyDamageMultiplier;//敌人易伤系数
    public float enemyAttackMultiplier;//敌人攻击系数
}

/// <summary>
/// 敌人类，实现敌人的行为和状态管理
/// </summary>
public class Enemy : MonoBehaviour
{
    private AIPath aiPath;
    private AIDestinationSetter aiDestinationSetter;
    
    private Character PlayerCharacter; // 玩家角色
    public Transform target; // 追击目标
    public Parameter parameters = new Parameter(); // 敌人属性实例
    public IState currentState; // 当前状态
    private Dictionary<StateType, IState> states = new Dictionary<StateType, IState>(); // 状态键值对集合
    private Rigidbody2D rb; // 刚体组件
    [Header("攻击")] 
    public LayerMask PlayerLayer; // 玩家图层

    /// <summary>
    /// 初始化敌人，设置初始状态和属性
    /// </summary>
    void Awake()
    {
        target= GameObject.FindGameObjectWithTag("Player").transform;
        
        
        parameters.enemyCurrentHealth = parameters.enemyMaxHealth;
        parameters.enemyAnimator = GetComponent<Animator>();
        //A*插件
        aiPath = GetComponent<AIPath>();
        aiDestinationSetter = GetComponent<AIDestinationSetter>();
        aiDestinationSetter.target = target;
        
        // 实例化各种状态并加入状态集合
        states.Add(StateType.Chase, new ChaseState(this));
        states.Add(StateType.Attack, new AttackState(this));
        states.Add(StateType.Hurt, new HurtState(this));
        states.Add(StateType.Death, new DeathState(this));
        states.Add(StateType.Cooldown, new CooldownState(this)); // 增加冷却状态

        //默认追击速度
        SetMoveSpeed(parameters.enemyMoveSpeed);
        // 默认状态设置为追击
        TransitionState(StateType.Chase);
        
        
    }

    /// <summary>
    /// 每帧更新状态和显示敌人当前血量
    /// </summary>
    void Update()
    {
        // 获取敌人水平移动方向
        float horizontalMove = target.position.x - transform.position.x;
        // 根据方向翻转敌人朝向
        if (horizontalMove > 0)
        {
            transform.localScale = new Vector3(1, 1, 1); // 面向右
        }
        else if (horizontalMove < 0)
        {
            transform.localScale = new Vector3(-1, 1, 1); // 面向左
        }
        // 调用当前状态的更新方法
        currentState.OnUpdate();
    }

    /// <summary>
    /// 每固定帧更新，预留方法
    /// </summary>
    void FixedUpdate()
    {
        //currentState.OnFixedUpdate();
    }

    /// <summary>
    /// 状态转换方法
    /// </summary>
    /// <param name="type">目标状态类型</param>
    public void TransitionState(StateType type)
    {
        currentState = states[type];
        currentState.OnEnter();
    }
    
    // 添加方法设置AIPath的maxSpeed
    public void SetMoveSpeed(float speed)
    {
        if (aiPath != null)
        {
            aiPath.maxSpeed = speed;
        }
    }

    /// <summary>
    /// 近战攻击动画事件，检测是否命中玩家
    /// </summary>
    public void MeleeAttackAnimEvent()
    {
        float realAttackValue = parameters.enemyAttackValue * parameters.enemyAttackMultiplier;
        Collider2D[] hitColiders = Physics2D.OverlapCircleAll(transform.position, parameters.enemyAttackDistance, PlayerLayer);
        foreach (Collider2D hitcolider in hitColiders)
        {
            hitcolider.GetComponent<Character>().TakeDamage(realAttackValue);
        }
    }

    /// <summary>
    /// 受伤方法，减少敌人血量
    /// </summary>
    /// <param name="damage">受到的伤害值</param>
    public void TakeDamage(float damage)
    {
        float realDamage = damage*parameters.enemyDamageMultiplier;
        parameters.enemyCurrentHealth -= realDamage; // 减少血量
        Debug.Log($"Enemy took {realDamage} damage, remaining health: {parameters.enemyCurrentHealth}");

        if (parameters.enemyCurrentHealth <= 0)
        {
            Die();
        }
    }

    /// <summary>
    /// 死亡方法，销毁敌人对象
    /// </summary>
    private void Die()
    {
        
        Debug.Log("Enemy died!");
        
        Destroy(gameObject); // 销毁敌人对象
    }
}
